#include "AIPlayer.h"
#include "AITeamState.h"
#include <math.h>
static vector2d posPlayer[MAX_PLAYER_TYPE];
static vector2d posToShoot[MAX_POS];


AIPlayer::AIPlayer(void)
{
	hasBall = false;
	defaultPos = currentPos = targetPos = vector2d(0, 0);
}

AIPlayer::~AIPlayer(void)
{

}

double AIPlayer::GetDistance2Ball()
{
	return distance2Ball;
}

bool AIPlayer::PassTo(vector2d posTo)
{
	//Khong nam trong kha nang sut bong
	if (distance2Ball > k_control_len )
	{

		//std::cout<<"Type:"<<typePlayer<<" distance "<<distance2Ball<<" can't shot!"<<std::endl;
		return false;
	}
	//Kiem tra doi thu co trong khu vuc hay ko
	area areaCheck(posTo, 200, 200);
	vector2d opntPos;
	if (CheckOpntInArea(areaCheck, opntPos) > 1)
	{
		int k = rand()%3 + 5;
		posTo = posPlayer[k];
		SetTarget(posTo);
		SetFoce(k_kick_foce);
		SetAction(KICK);
		return true;
		//std::cout<<"Too many oponent in area to pass!\n";
	}

	////Co the d+a bong
	//std::cout<<"Pass to "<<posTo.x<<" : "<<posTo.y<<std::endl;
	std::vector<Player*> opntTeam = AITeamState::GetInstance()->opnt_team;
	std::vector<Player*>::iterator opnPlayer;
	Ball* ball = AITeamState::GetInstance()->footBallMatch->GameBall();
	bool canPass = true;
	//Can pass straight
	for(opnPlayer = opntTeam.begin();opnPlayer != opntTeam.end();opnPlayer++)
	{
		if((*opnPlayer)->Pos().InsideRegion(currentPos,posTo)) {
			canPass = false;
			break;
		}
	}
	if (canPass)
	{
		SetTarget(posTo);
		SetFoce(k_kick_foce);
		SetAction(KICK);
		double v = sqrt( -2.0f * 0.1f * (posTo - currentPos).Length() + 100.0f);
		AITeamState::GetInstance()->ResetTimeCount((10.0f - v)/0.1f);
		return true;
	}
	//Chien thuat bat tuong
	//Tinh diem cham tuong
	vector2d posO;
	double d = fabs(posTo.x - currentPos.x);
	double x = (posTo.y * d) / (posTo.y + currentPos.y);
	posO.x = posTo.x > currentPos.x ? posTo.x - x : posTo.x + x;
	posO.y = currentPos.y < PITCH_CENTER_POINT.y ? 0 : PITCH_HEIGHT;
	//Quang duong bong di chuyen
	double s = ball->Velocity() * ball->Velocity() / ( 2 * 0.1f); 
	//Gia su luc truyen MAX de co van toc ban dau la 10
	//Quang duong can thiet
	vector2d s1(posTo.x - posO.x, posTo.y - posO.y);
	vector2d s2(currentPos.x - posO.x, currentPos.y - posO.y);
	if (s < (s1.Length() + s2.Length())) 
	{
		return false;
	}
	else
	{
		SetTarget(posO);
		SetFoce(k_kick_foce);
		SetAction(KICK);
		double velocity = sqrt(- 2.0f * 0.1f * (s1.Length() + s2.Length()) + 100.0f);
		AITeamState::GetInstance()->ResetTimeCount((10.0f - velocity)/0.1f);
	}
	return true;
}
double AIPlayer::GetFoce()
{
	return this->foce;
}
void AIPlayer::SetTarget(vector2d target)
{
	this->targetPos = target;
}
void AIPlayer::ReturnDefaultPos()
{
	this->targetPos= defaultPos;
	this->action = MOVE;
}
void AIPlayer::Update()
{
	if (fabs((currentPos - targetPos).Length()) < 10.0f)
	{
		SetTarget(targetPos);
		SetFoce(0);
		SetAction(MOVE);
	}
}
void AIPlayer::SetFoce(double _foce)
{
	this->foce = _foce;
}
vector2d AIPlayer::GetPos()
{
	return this->currentPos;
}
vector2d AIPlayer::GetTargetPos()
{
	return this->targetPos;
}
void AIPlayer::SetPos(vector2d _pos)
{
	this->currentPos = _pos;

}
void AIPlayer::SetTypePlayer(PLAYERTYPE _typePlayer)
{
	this->typePlayer = _typePlayer;
}
bool AIPlayer::IsMoveTo(vector2d _pos)
{
		vector<Player*>::iterator opnt_it;
		bool check = true;
		for(opnt_it = AITeamState::GetInstance()->opnt_team.begin();opnt_it != AITeamState::GetInstance()->opnt_team.end(); opnt_it++)
		{
			if(DistanceXY((*opnt_it)->Pos(), _pos) < AREA_SAFE_CHECK)
			{
				check = false;
			}
		}
		if(check) 
		{
			return true;
		}
		return check;
}
void AIPlayer::MoveTo(vector2d _pos)
{
		SetFoce(k_move_foce);
		SetAction(MOVE);
		vector<Player*>::iterator opnt_it;
		bool check = true;
		for(opnt_it = AITeamState::GetInstance()->opnt_team.begin();opnt_it != AITeamState::GetInstance()->opnt_team.end(); opnt_it++)
		{
			if(DistanceXY((*opnt_it)->Pos(), _pos) < AREA_SAFE_CHECK)
			{
				check = false;
			}
		}
		if(check) 
		{
			SetTarget(_pos);
		}
}
bool AIPlayer::CanPass(vector2d target,vector2d pos_pass)
	{
		vector<Player*>::iterator opnt_it;
		for(opnt_it= AITeamState::GetInstance()->opnt_team.begin();opnt_it != AITeamState::GetInstance()->opnt_team.end(); opnt_it++)
		{
			if((*opnt_it)->Pos().InsideRegion(target,pos_pass)	)return false;
		}
		return true;
	}
void AIPlayer::MoveTo(PLAYERTYPE type, vector2d pos_pass)
{
	//return;
	SetTarget(posPlayer[type]);
	SetFoce(k_kick_foce);
	SetAction(MOVE);
	return;
	switch(type)
	{
		case(ATTACKPLAYER):
			{
			vector2d target = posPlayer[type];
			target.x = (posPlayer[type].x + AITeamState::GetInstance()->footBallMatch->Goal_Opnt().x)/2;
			if(!CanPass(target, pos_pass))
			{
				if(target.x < AITeamState::GetInstance()->footBallMatch->GameBall()->Pos().x)
				{
					target.y += AREA_SAFE_CHECK;
				}
				else
				{
					target.y -= AREA_SAFE_CHECK;
				}
			}
			
			break;}
		case(ATTACKPLAYERLEFT):{
			vector2d target = posPlayer[type];
			target.x = (posPlayer[type].x + AITeamState::GetInstance()->footBallMatch->Goal_Opnt().x)/2;
			if(!CanPass(target, pos_pass))
			{
				target.y += 200;
			}
			break;}
		case(ATTACKPLAYERRIGHT):{
			vector2d target = posPlayer[type];
			target.y = (posPlayer[type].y + AITeamState::GetInstance()->footBallMatch->Goal_Opnt().y)/2;
			if(!CanPass(target, pos_pass))
			{
				target.y -= AREA_SAFE_CHECK;
			}
			break;}
		default:{
			vector2d target = posPlayer[type];
			if(!CanPass(target, pos_pass))
			{
				target.x = (target.x + AITeamState::GetInstance()->footBallMatch->GameBall()->Pos().x)/2;
			}
			break;}
				SetFoce(k_move_foce);
				SetAction(MOVE);
	}
		/*bool check = false;
		
		vector2d target = posPlayer[type];
		if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

		
		check = CanPass(target, pos_pass); 
		if(check)
		{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(0, AREA_SAFE_CHECK);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(0, -AREA_SAFE_CHECK);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(AREA_SAFE_CHECK, 0);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(-AREA_SAFE_CHECK, 0);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(AREA_SAFE_CHECK, AREA_SAFE_CHECK);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(-AREA_SAFE_CHECK, -AREA_SAFE_CHECK);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(-AREA_SAFE_CHECK, AREA_SAFE_CHECK);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			check = CanPass(target, pos_pass); 
			if(check)
			{
				SetTarget(target);
				SetAction(MOVE);
				SetFoce(k_move_foce);
			}
		}
		if(!check)
		{
			target = posPlayer[type] + vector2d(AREA_SAFE_CHECK, -AREA_SAFE_CHECK);
			if(target.x < 0.0) target.x = 0.0;
			if(target.x > PITCH_WIDTH) target.x = PITCH_WIDTH;
			if(target.y < 0.0) target.y = 0.0;
			if(target.y > PITCH_HEIGHT) target.y = PITCH_HEIGHT;

			SetTarget(target);
			SetAction(MOVE);
			SetFoce(k_move_foce);
			
		}*/
}
double DistanceXY(vector2d v1, vector2d v2)
{
	return sqrt((v1.x-v2.x)*(v1.x-v2.x) + (v1.y-v2.y)* (v1.y-v2.y));
}
void AIPlayer::Support()
{
	Ball *ball = AITeamState::GetInstance()->footBallMatch->GameBall();
	if(!this->isClosest)
	if(DistanceXY(GetPos(), ball->Pos()) < 100)
	{
		SetFoce(k_move_foce);
		SetAction(MOVE);
		SetTarget( ball->Pos());
		
	}
}

void AIPlayer::InitPlayerType(int side)
{
	if (side == LEFT)
	{
		posPlayer[GOAL_KEEPER] = vector2d(GOAL_LEFT_CENTER.x - 70, PITCH_HEIGHT/2);
		posPlayer[LEFTPLAYER] = vector2d(PITCH_WIDTH/4, PITCH_HEIGHT/6);
		posPlayer[RIGHTPLAYER] = vector2d(PITCH_WIDTH/4, PITCH_HEIGHT/6 * 5);
		posPlayer[CENTERPLAYER] = vector2d(PITCH_WIDTH/2, PITCH_HEIGHT/2);
		posPlayer[DEFENCEPLAYER] = vector2d(PITCH_WIDTH/6, PITCH_HEIGHT/2);
		posPlayer[ATTACKPLAYER] = vector2d(PITCH_WIDTH/6 * 5, PITCH_HEIGHT/2);
		posPlayer[ATTACKPLAYERLEFT] = vector2d(PITCH_WIDTH/4 * 3, PITCH_HEIGHT/6);
		posPlayer[ATTACKPLAYERRIGHT] = vector2d(PITCH_WIDTH/4 * 3, 3 * PITCH_HEIGHT/6 * 5);
	}
	else
	{
		posPlayer[GOAL_KEEPER] = vector2d(PITCH_WIDTH, PITCH_HEIGHT/2);
		posPlayer[LEFTPLAYER] = vector2d(PITCH_WIDTH/4 * 3, 5 * PITCH_HEIGHT/6);
		posPlayer[RIGHTPLAYER] = vector2d(PITCH_WIDTH/4 * 3, PITCH_HEIGHT/6);
		posPlayer[CENTERPLAYER] = vector2d(PITCH_WIDTH/2, PITCH_HEIGHT/2);
		posPlayer[DEFENCEPLAYER] = vector2d(PITCH_WIDTH/6 * 5, PITCH_HEIGHT/2);
		posPlayer[ATTACKPLAYER] = vector2d(PITCH_WIDTH/6, PITCH_HEIGHT/2);
		posPlayer[ATTACKPLAYERLEFT] = vector2d(PITCH_WIDTH/4, 5 * PITCH_HEIGHT/6);
		posPlayer[ATTACKPLAYERRIGHT] = vector2d(PITCH_WIDTH/4, PITCH_HEIGHT/6);
	}
}

void AIPlayer::ResetPosToShoot(int side)
{
	int xPos = side == LEFT ? PITCH_WIDTH : 0;
	int distance = GOAL_SIZE / MAX_POS;
	int yPos0 = GOAL_LEFT_CENTER.y - GOAL_SIZE/2;
	for (int i = 0; i < MAX_POS; i++)
	{
		posToShoot[i].y = yPos0 + distance * i;
		posToShoot[i].x = xPos;
	}
	int delta = 10;
	posToShoot[0].y += delta;
	posToShoot[MAX_POS - 1].y -= delta;
	for (int i = 0; i < MAX_POS; i++)
	{
		std::cout<<"Pos "<<i<<" : ("<<posToShoot[i].x<<","<<posToShoot[i].y<<")\n";
	}
}
void AIPlayer::SetAction(int _action)
{
	this->action = _action;
}
void AIPlayer::SetDistance2Ball(double _distance)
{
	this->distance2Ball = _distance;
}
PLAYERTYPE AIPlayer::GetTypePlayer()
{
	return this->typePlayer;
}
void AIPlayer::StandAt(PLAYERTYPE type)
{
	AITeamState* state = AITeamState::GetInstance();
	PLAYERTYPE posTypeOfBall = state->GetAreaOfBall();
	vector2d posOfBall = state->footBallMatch->GameBall()->Pos();
	vector2d posOfMyGoal = state->footBallMatch->Goal_Team();
	vector2d posOfOpnGoal = state->footBallMatch->Goal_Opnt();
	vector2d targetStand;
	int sign;
	//std::cout<<"Pos of Ball: "<<posTypeOfBall<<" pos of Player "<<type<<"\n";
	switch(posTypeOfBall)
	{
	case (LEFTPLAYER):
	case (RIGHTPLAYER):
		sign = (posTypeOfBall == LEFTPLAYER) ? -1 : 1;
		if (type == DEFENCEPLAYER)
		{
			targetStand = PointIntersect(posOfMyGoal, posOfBall, PITCH_HEIGHT/2.0f + sign * PITCH_HEIGHT/6.0f , false);
		}
		if (type == ATTACKPLAYERLEFT || type == ATTACKPLAYERRIGHT)
		{
			targetStand = PointIntersect(posPlayer[type], posOfBall, 3.0f /8.0f * PITCH_WIDTH, true); 
		}
		if (type == CENTERPLAYER)
		{
			targetStand = PointIntersect(posPlayer[type], posOfBall, PITCH_WIDTH/4.0f, true); 
			if (posTypeOfBall == LEFTPLAYER)
			{
				if(targetStand.y < PITCH_HEIGHT/3)
					targetStand.y = PITCH_HEIGHT/3;
			}
			else if (targetStand.y > 2*PITCH_HEIGHT/3)
			{
				targetStand.y = 2*PITCH_HEIGHT/3;
			}
		}
		break;
	case (DEFENCEPLAYER):
		if (type == RIGHTPLAYER || type == LEFTPLAYER)
		{
			targetStand = posPlayer[type];
		}
		if (type == ATTACKPLAYER)
		{
			targetStand = PointIntersect(posPlayer[type], posPlayer[DEFENCEPLAYER], PITCH_WIDTH/3.0f, true); 
		}
		break;
	case (ATTACKPLAYERLEFT):
	case (ATTACKPLAYERRIGHT):
		sign = (posTypeOfBall == ATTACKPLAYERLEFT) ? -1 : 1;
		if (type == ATTACKPLAYER)
		{
			targetStand = PointIntersect(posOfMyGoal, posOfBall, PITCH_HEIGHT/2.0f + sign * PITCH_HEIGHT/6.0f , false);
		}
		if (type == LEFTPLAYER || type == RIGHTPLAYER)
		{
			targetStand = PointIntersect(posPlayer[type], posOfBall, 5.0f /8.0f * PITCH_WIDTH, true); 
		}
		if (type == CENTERPLAYER)
		{
			targetStand = PointIntersect(posPlayer[type], posOfBall, 3.0f*PITCH_WIDTH/4.0f, true);
			if (posTypeOfBall == ATTACKPLAYERLEFT)
			{
				if (targetStand.y < PITCH_HEIGHT/3)
					targetStand.y = PITCH_HEIGHT/3;
			}
			else if (targetStand.y > 2*PITCH_HEIGHT/3)
				targetStand.y = 2*PITCH_HEIGHT/3;
		}
		break;
	case (ATTACKPLAYER):
		sign = (type == ATTACKPLAYERLEFT) ? -1 : 1;
		if (type == ATTACKPLAYERLEFT || type == ATTACKPLAYERRIGHT)
		{
			targetStand = PointIntersect(posPlayer[type], posOfOpnGoal, PITCH_HEIGHT/2.0f + sign * PITCH_HEIGHT/4.0f, false); 
		}
		if (type == DEFENCEPLAYER)
		{
			targetStand = PointIntersect(posPlayer[type], posOfOpnGoal, 2.0f * PITCH_WIDTH/3.0f, true); 
		}
		break;
	case (CENTERPLAYER):
		if (type == ATTACKPLAYER)
		{
			targetStand = PointIntersect(posPlayer[type], posPlayer[CENTERPLAYER], 2.0f * PITCH_WIDTH/3.0f, true); 
		}
		if (type == LEFTPLAYER || type == RIGHTPLAYER)
		{
			sign = (type == LEFTPLAYER) ? -1 : 1;
			targetStand = PointIntersect(posPlayer[type], posOfBall, PITCH_HEIGHT/2.0f + sign * PITCH_HEIGHT/6.0f, false); 
		}
		break;
	};
	//std::cout<<"Target Stand : ("<<targetStand.x<<","<<targetStand.y<<")"<<std::endl;
	SetTarget(targetStand);
	SetFoce(k_move_foce);
	SetAction(MOVE);
}
/*************************************************
*	Return TRUE if has any opponent player inside the Range of player 
*	The Range define radius = k_threaten_len
***************************************************/
bool AIPlayer::BeThreaten(std::vector<Player*> Opnt_team,Player* player){
		
		vector2d vdist;
		vector<Player*>::iterator opnt_it;
		for(opnt_it= Opnt_team.begin();opnt_it != Opnt_team.end(); opnt_it++)
		{
			vdist = (*opnt_it)->Pos() - player->Pos();
			if(vdist.Length() <= k_threaten_len) return true;
		}
		return false;

	}
int AIPlayer::GetAction()
{
	return this->action;
}
void AIPlayer::AutoMove()
{
	Ball *ball = AITeamState::GetInstance()->footBallMatch->GameBall();
	if(DistanceXY(GetPos(), ball->Pos()) < 100)
	{
		SetTarget(ball->Pos());
	}
	else
	{
		SetTarget(ball->Pos()+ball->Head()*(ball->Force()*50));
	}
	SetFoce(k_move_foce);
	SetAction(MOVE);
}
void AIPlayer::SetDefaultPos()
{
	targetPos = defaultPos = posPlayer[typePlayer];
	action = MOVE;
	foce = k_kick_foce;
}
bool AIPlayer::Shoot(bool isShoot)
{
	if (distance2Ball > k_control_len)
	{
		return false;
	}
	Player* goalKeeper = AITeamState::GetInstance()->opnt_team.at(0);
	if (isShoot)
	{
		int max = -PITCH_WIDTH, iShoot = -1;
		bool canShoot = false;
		for (int i = 0; i < MAX_POS; i++)
		{
			if (CanPass(posToShoot[i]) && (DistanceXY(posToShoot[i], goalKeeper->Pos()) > max))
			{
				iShoot = i;
				canShoot = true;
				max = DistanceXY(posToShoot[i], goalKeeper->Pos());
			}
		}
		if (canShoot )
		{
			SetTarget(posToShoot[iShoot]);
			SetFoce(k_kick_foce);
			SetAction(KICK);
		}
		return canShoot;
	}
	else
	{
		int max = PITCH_WIDTH, iShoot = -1;
		for (int i = 0; i < MAX_POS; i++)
		{
			if (DistanceXY(posToShoot[i], goalKeeper->Pos()) < max)
			{
				iShoot = i;
			}
		}
		SetTarget(posToShoot[iShoot]);
		SetFoce(k_kick_foce);
		SetAction(KICK);
		return true;
	}
}

bool AIPlayer::CanPass(vector2d target)
{
	vector<Player*>::iterator opnt_it;
	for(opnt_it= AITeamState::GetInstance()->opnt_team.begin();opnt_it != AITeamState::GetInstance()->opnt_team.end(); opnt_it++)
	{
		if((*opnt_it)->Pos().InsideRegion(currentPos, target)) return false;
	}
	return true;
}

bool AIPlayer::CatchBall()
{
	Ball* ball = AITeamState::GetInstance()->footBallMatch->GameBall();
	return true;
}

bool AIPlayer::GetBall()
{
	//Get ball success
	//std::cout<<"Player "<<typePlayer<<" will get ball\n";
	if (distance2Ball < k_control_len)
	{
		return true;
	}

	Ball* ball = AITeamState::GetInstance()->footBallMatch->GameBall();
	double tmax = (ball->Velocity()/0.1f);
	double velocityOfPlayer = 5.0f;
	bool canGet = false;
	double vBall = ball->Velocity();
	while (vBall > ball->Velocity()/2.0f)
	{
		vBall = vBall - 0.1f;
		//double s = (vBall * i - 0.5f * 0.1f * i * i);
		double s = (ball->Velocity() * ball->Velocity() - vBall * vBall)/0.2f;
		double deltaY = s * sin(0.01745f * ball->Angle());
		double deltaX = s * cos(0.01745f * ball->Angle());
		vector2d tmpPoint = vector2d(ball->Pos().x + deltaX, ball->Pos().y + deltaY);
		vector2d dis2ball = tmpPoint - currentPos;
		double deltaV = fabs(dis2ball.Length()/velocityOfPlayer - (ball->Velocity() - vBall)/0.1f);
		//std::cout<<"S = "<<s<<" Dis2ball :"<<dis2ball.Length()<<" Delta V = "<<deltaV<<std::endl;
		if ( deltaV < 3.0f)
		{
			SetTarget(tmpPoint);
			SetFoce(k_kick_foce);
			SetAction(MOVE);
			canGet = true;
			//std::cout<<"Can get ball success! Delta = "<<deltaV<<"\n";
			AITeamState::GetInstance()->ResetTimeCount(dis2ball.Length()/velocityOfPlayer);
			return true;
		}
	}        
	if (!canGet)
	{
		SetTarget(ball->Pos());
		SetFoce(k_kick_foce);
		SetAction(MOVE);
	}
	return true;
}
bool AIPlayer::IsCanGetBall()
{
	Ball* ball = AITeamState::GetInstance()->footBallMatch->GameBall();
	vector2d dis2ball = ball->Pos() - posPlayer[typePlayer];
	if (fabs(dis2ball.x) < AREA_ACTIVITE_WIDTH && fabs(dis2ball.y) < AREA_ACTIVITE_HEIGHT)
		return true;
	return false;
	//return AITeamState::GetInstance()->GetAreaOfBall() == typePlayer;
}

vector2d AIPlayer::GetPositionOfType(PLAYERTYPE type)
{
	return posPlayer[type];
}

double AIPlayer::GetAngleBetween(vector2d pos1, vector2d pos2)
{
	return fabs(atan((pos1.y - currentPos.y)/(pos1.x - currentPos.x)) - atan((pos2.y - currentPos.y)/(pos2.x - currentPos.x))); 
}

int AIPlayer::CheckOpntInArea(area areatoCheck, vector2d& posOfOpnt)
{
	vector<Player*>::iterator opnt_it;
	int numberOf = 0;
	for(opnt_it= AITeamState::GetInstance()->opnt_team.begin();opnt_it != AITeamState::GetInstance()->opnt_team.end(); opnt_it++)
	{
		vector2d dis = (*opnt_it)->Pos() - areatoCheck.point;
		if (fabs(dis.x) <= areatoCheck.width/2.0f && fabs(dis.y) <= areatoCheck.height/2.0f)
		{
			posOfOpnt = (*opnt_it)->Pos();
			numberOf++;
		}
	}
	//std::cout<<"Number of opnt = "<<numberOf<<std::endl;
	return numberOf;
}
vector2d AIPlayer::PointIntersect(vector2d pos1, vector2d pos2, double point, bool x)
{
	vector2d p = pos1 - pos2;
	vector2d intersect;
	if (x)
	{
		intersect.x = point;
		intersect.y = p.y/p.x*(point - pos2.x) + pos2.y;
	}
	else{
		intersect.y = point;
		intersect.x = p.x/p.y*(point - pos2.y) + pos2.x;
	}
	//std::cout<<" pos1 ("<<pos1.x<<","<<pos1.y<<") ; pos2 ("<<pos2.x<<" , "<<pos2.y<<") point :"<<point<<" x= "<<x<<std::endl;
	return intersect;
}